// 控制并发

function ajax(time) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (time === 2000) {
        reject()
      }
      resolve()
    }, time)
  })
}


// ajax(1000)
// ajax(2000)
// ...
class Limit {
  constructor(paralleCount = 2) {
    this.tasks = []
    this.runningCount = 0
    this.paralleCount = paralleCount
  }

  add(task) {   // 添加任务
    return new Promise((resolve, reject) => {
      this.tasks.push({
        task,
        resolve,
        reject
      })

      this._run()
    })
  }

  _run() {   // 执行任务
    while (this.runningCount < this.paralleCount && this.tasks.length) {
      const { task, resolve, reject } = this.tasks.shift()
      this.runningCount++

      task().then(() => {
        resolve()
        this.runningCount--
        this._run()
      })
        .catch(() => {
          reject()
          this.runningCount--
          this._run()
        })
    }
  }
}

let limit = new Limit(2)

function addTask(time, name) {
  limit
    .add(() => ajax(time))
    .then(() => {
      console.log(`任务${name}完成`);
    })
    .catch(() => {
      console.log(`任务${name}失败`);

    })
}

addTask(10000, 1)
addTask(2000, 2)
addTask(5000, 3)
addTask(1000, 4)
addTask(7000, 5)